package com.example.su_shi_tan_bas_lib.album.media_reader;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import androidx.annotation.WorkerThread;

import com.example.su_shi_tan_bas_lib.R;
import com.example.su_shi_tan_bas_lib.album.bean.AlbumFile;
import com.example.su_shi_tan_bas_lib.album.bean.AlbumFolder;
import com.example.su_shi_tan_bas_lib.global.AppGlobals;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ImageMediaReader implements AlbumMediaReader {

    /**
     * Image attribute.
     */
    private static final String[] IMAGES = {
            MediaStore.Images.Media.DATA,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
            MediaStore.Images.Media.MIME_TYPE,
            MediaStore.Images.Media.DATE_ADDED,
            MediaStore.Images.Media.LATITUDE,
            MediaStore.Images.Media.LONGITUDE,
            MediaStore.Images.Media.SIZE
    };



    @Override
    @WorkerThread
    public void scanMediaFile(Map<String, AlbumFolder> albumFolderMap, AlbumFolder allFileFolder) {
        Context context = AppGlobals.getApplication();
        ContentResolver contentResolver = context.getContentResolver();
        Cursor cursor = contentResolver.query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                IMAGES,
                null,
                null,
                null
        );
        if (cursor == null) {
            return;
        }
        while (cursor.moveToNext()) {
            String path = cursor.getString(0);
            String bucketName = cursor.getString(1);
            String mimeType = cursor.getString(2);
            long addDate = cursor.getLong(3);
            float latitude = cursor.getFloat(4);
            float longitude = cursor.getFloat(5);
            long size = cursor.getLong(6);

            AlbumFile imageFile = new AlbumFile();
            imageFile.setMediaType(AlbumFile.TYPE_IMAGE);
            imageFile.setPath(path);
            imageFile.setBucketName(bucketName);
            imageFile.setMimeType(mimeType);
            imageFile.setAddDate(addDate);
            imageFile.setLatitude(latitude);
            imageFile.setLongitude(longitude);
            imageFile.setSize(size);
//
//            if (mSizeFilter != null && mSizeFilter.filter(size)) {
//                if (!mFilterVisibility) continue;
//                imageFile.setDisable(true);
//            }
//            if (mMimeFilter != null && mMimeFilter.filter(mimeType)) {
//                if (!mFilterVisibility) continue;
//                imageFile.setDisable(true);
//            }

            allFileFolder.addAlbumFile(imageFile);
            AlbumFolder albumFolder = albumFolderMap.get(bucketName);

            if (albumFolder != null)
                albumFolder.addAlbumFile(imageFile);
            else {
                albumFolder = new AlbumFolder();
                albumFolder.setName(bucketName);
                albumFolder.addAlbumFile(imageFile);

                albumFolderMap.put(bucketName, albumFolder);
            }
        }
        cursor.close();
    }

    @Override
    @WorkerThread
    public List<AlbumFolder> getAllMediaFile() {
        Map<String, AlbumFolder> albumFolderMap = new HashMap<>();
        AlbumFolder allFileFolder = new AlbumFolder();
        allFileFolder.setChecked(true);
        Context context = AppGlobals.getApplication();
        String name=context.getString(R.string.album_all_images);
        allFileFolder.setName(name);

        scanMediaFile(albumFolderMap, allFileFolder);

        ArrayList<AlbumFolder> albumFolders = new ArrayList<>();
        Collections.sort(allFileFolder.getAlbumFiles());
        albumFolders.add(allFileFolder);

        for (Map.Entry<String, AlbumFolder> folderEntry : albumFolderMap.entrySet()) {
            AlbumFolder albumFolder = folderEntry.getValue();
            Collections.sort(albumFolder.getAlbumFiles());
            albumFolders.add(albumFolder);
        }
        return albumFolders;
    }
}
